package com.sxfq.loansupermarket.backsystem.modular.basicData.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.sxfq.loansupermarket.backsystem.common.constant.factory.PageFactory;
import com.sxfq.loansupermarket.backsystem.common.constant.tips.Tip;
import com.sxfq.loansupermarket.backsystem.common.controller.BaseController;
import com.sxfq.loansupermarket.backsystem.common.exception.BizExceptionEnum;
import com.sxfq.loansupermarket.backsystem.common.exception.BussinessException;
import com.sxfq.loansupermarket.backsystem.core.log.LogManager;
import com.sxfq.loansupermarket.backsystem.core.log.factory.LogTaskFactory;
import com.sxfq.loansupermarket.backsystem.core.shiro.ShiroKit;
import com.sxfq.loansupermarket.backsystem.core.support.HttpKit;
import com.sxfq.loansupermarket.backsystem.core.util.ToolUtil;
import com.sxfq.loansupermarket.backsystem.modular.basicData.entity.CashDailyMethodInfo;
import com.sxfq.loansupermarket.backsystem.modular.basicData.entity.CheckoutMethodInfo;
import com.sxfq.loansupermarket.backsystem.modular.basicData.entity.SearchCashDailyMethodInfo;
import com.sxfq.loansupermarket.backsystem.modular.basicData.entity.SearchCheckoutMethodInfo;
import com.sxfq.loansupermarket.backsystem.modular.basicData.service.ICheckoutMethodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * 收费方式管理
 *
 * @author py
 * @Date 2018年4月19日23:15:54
 */
@Controller
@RequestMapping("/checkoutMethod")
public class CheckoutMethodController extends BaseController {

    @Autowired
    ICheckoutMethodService service;

    private String PREFIX = "/basicData/checkoutMethod/";

    /**
     * 跳转到性格管理页面
     */
    @RequestMapping("")
    public String checkoutMethod(Model model) {
        return PREFIX + "/checkoutMethod.html";
    }


    /**
     * 跳转现金日报表界面
     */
    @RequestMapping("/cash")
    public String cashDaily(Model model) {
        String tableHead = ShiroKit.getUser().getAbbreviation();
        if (tableHead == "" || tableHead == null) {
            LogManager.me().executeLog(LogTaskFactory.exitLog(ShiroKit.getUser().getId(), HttpKit.getIp()));
            ShiroKit.getSubject().logout();
            return "/login.html";
        }
        return PREFIX + "/cashDailyMethod.html";
    }

    /**
     * 跳转到预约客户管理添加页面
     */
    @RequestMapping(value = "/checkoutMethod_add")
    public String checkoutMethodAdd() {
        return PREFIX + "checkoutMethod_add.html";
    }

    /**
     * 跳转到预约客户管理添加页面
     */
    @RequestMapping(value = "/cashMethod_add")
    public String cashMethodAdd() {
        return PREFIX + "cashDailyMethod_add.html";
    }


    /**
     * 跳转到性格管理详情页面
     */
    @RequestMapping(value = "/checkoutMethod_update/{checkoutMethodId}")
    public String checkoutMethodUpdate(@PathVariable String checkoutMethodId, Model model) {
        if (ToolUtil.isEmpty(checkoutMethodId)) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        CheckoutMethodInfo checkoutMethodInfo = this.service.selectOneById(checkoutMethodId);
        model.addAttribute("checkoutMethodInfo", checkoutMethodInfo);
        return PREFIX + "checkoutMethod_edit.html";
    }

    /**
     * 跳转到性格管理详情页面22
     */
    @RequestMapping(value = "/cashMethod_update/{checkoutMethodId}")
    public String cashMethodUpdate(@PathVariable String checkoutMethodId, Model model) {
        if (ToolUtil.isEmpty(checkoutMethodId)) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        CashDailyMethodInfo cashDailyMethodInfo = this.service.selectById(checkoutMethodId);
        model.addAttribute("cashDailyMethodInfo", cashDailyMethodInfo);
        return PREFIX + "cashDailyMethod_edit.html";
    }

    /**
     * 性格管理列表
     */
    @RequestMapping("/list")
    @ResponseBody
    public Object testrInfoList(SearchCheckoutMethodInfo info, HttpServletRequest request) {
        Page<CheckoutMethodInfo> page = new PageFactory<CheckoutMethodInfo>().defaultPage();
        try {
            List<CheckoutMethodInfo> list = service.selectPageListByQueryGetMap(page, info);
            page.setRecords(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return super.packForBT(page);
    }

    /**
     * 现金日报表
     */
    @RequestMapping("/cashList")
    @ResponseBody
    public Object cashList(SearchCashDailyMethodInfo info, HttpServletRequest request) {
        Page<CashDailyMethodInfo> page = new PageFactory<CashDailyMethodInfo>().defaultPage();
        try {
            List<CashDailyMethodInfo> list = service.selectPageListByQuery(page, info);
            page.setRecords(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return super.packForBT(page);
    }

    /**
     * 添加性格
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public int add(@Valid CheckoutMethodInfo info, BindingResult result) throws Exception {
        if (result.hasErrors()) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        return service.insert(info);
    }

    /**
     * 现金日报添加
     */
    @RequestMapping(value = "/cashAdd")
    @ResponseBody
    public int cashAdd(@Valid CashDailyMethodInfo info, BindingResult result) throws Exception {
        if (result.hasErrors()) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        return service.insert(info);
    }


    /**
     * 删除现金日报收收费方式
     */
    @RequestMapping(value = "/deletes/{id}")
    @ResponseBody
    public Tip deletes(@PathVariable String id) throws Exception {
        service.deletes(id);
        return SUCCESS_TIP;
    }

    /**
     * 删除收费方式
     */
    @RequestMapping(value = "/delete/{id}")
    @ResponseBody
    public Tip delete(@PathVariable String id) throws Exception {
        service.delete(id);
        return SUCCESS_TIP;
    }

    /**
     * 修改考题
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public int update(@Valid CheckoutMethodInfo info, BindingResult result) throws Exception {
        if (result.hasErrors()) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        return service.update(info);
    }

    /**
     * 修改考题
     */
    @RequestMapping(value = "/cashUpdate")
    @ResponseBody
    public int cashUpdate(@Valid CashDailyMethodInfo info, BindingResult result) throws Exception {
        if (result.hasErrors()) {
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        return service.update(info);
    }



    @RequestMapping(value = "/getList", method = RequestMethod.POST)
    @ResponseBody
    public  List<Map<String,Object>> getList() {
        List<Map<String,Object>> list= service.list();
        return list;
    }
}
